package com.compomics.proteocloud.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.jets3t.service.security.AWSCredentials;

public class AWSUtils {
		private static final String PROPERTIES_FILE = "conf/proteocloud.properties2";
	    public static final String AWS_ACCESS_KEY_PROPERTY_NAME = "awsAccessKey";
	    public static final String AWS_SECRET_KEY_PROPERTY_NAME = "awsSecretKey";	    
	    public static final String DEFAULT_WORKER_IMAGE = "ami";
	    public static final String DEFAULT_KEYPAIR = "keypair";
	    public static final String DEFAULT_SECURITY_GROUP = "group";
	    public static final String BUCKET = "bucket";
        public static final String ZONE = "zone";
	    public static final String DATABASE_URL = "dbUrl";
	    public static final String DATABASE_USER = "dbUser";
	    public static final String DATABASE_PASS = "dbPass";
	    public static final String DATABASE_NAME = "dbName";
	    public static final String PROJECT_TITLE = "project";
	    public static final String PREC_IONTOL = "precIonTol";
	    public static final String FRAG_IONTOL = "fragIonTol";	    
	    
	    /**
	     * The zones for the instance launch.
	     */
	    public static final String[] ZONES = {"us-east-1a", "us-east-1b", "us-east-1c", "us-east-1d"};
	    /**
		 * The available taxa to search.
		 */
		public static final String[] TAXA = {"Human (human)", "Mouse (mouse)", "Mycobacterium (mycobacterium)"};
		private static Properties properties;
		
		/**
		 * Returns the Properties object.
		 * @return
		 */
	    public static Properties getProperties() {
			return properties;
		}
	    
	    /**
	     * Sets the Credentials retrieved from the user interface.
	     * @param credentials
	     */
	    public static void setCredentials(Credentials credentials){
	    	if(properties == null) properties = new Properties();
	    	properties.setProperty(AWS_ACCESS_KEY_PROPERTY_NAME, credentials.getAccesskey());
	    	properties.setProperty(AWS_SECRET_KEY_PROPERTY_NAME, credentials.getSecretaccesskey());
	    	properties.setProperty(DEFAULT_KEYPAIR, credentials.getKeypair());
	    	properties.setProperty(DEFAULT_SECURITY_GROUP, credentials.getGroup());
	    }
	    
	    /**
	     * Sets the ProjectSettings retrieved from the user interface.
	     * @param settings
	     */
	    public static void setProjectSettings(ProjectSettings settings){
	    	if(properties == null) properties = new Properties();  
	    	properties.setProperty(DATABASE_URL, settings.getDbUrl());
	    	properties.setProperty(DATABASE_USER, settings.getDbUser());
	    	properties.setProperty(DATABASE_PASS, settings.getDbPass());
	    	properties.setProperty(DATABASE_NAME, settings.getDbName());
	    	
	    }
	    
		/**
	     * Loads AWS Credentials from the file <tt>samples.properties</tt>
	     * ({@link #PROPERTIES_FILE}) that must be available in the
	     * classpath, and must contain settings <tt>awsAccessKey</tt> and
	     * <tt>awsSecretKey</tt>.
	     *
	     * @return the AWS credentials loaded from the samples properties file.
	     */
	    public static AWSCredentials loadAWSCredentials() throws IOException, RuntimeException {

            File propsfile = new File(PROPERTIES_FILE);

	        InputStream propertiesIS = new FileInputStream(propsfile);
            
	        if (propertiesIS == null) {
	            throw new RuntimeException("Unable to load properties file from classpath: ");
	        }

	        properties = new Properties();
	        properties.load(propertiesIS);


	        AWSCredentials awsCredentials = null;
            if(properties.getProperty(AWS_SECRET_KEY_PROPERTY_NAME).length() > 0 && properties.getProperty(AWS_SECRET_KEY_PROPERTY_NAME).length() > 0){
                awsCredentials = new AWSCredentials(
                        properties.getProperty(AWS_ACCESS_KEY_PROPERTY_NAME),
                        properties.getProperty(AWS_SECRET_KEY_PROPERTY_NAME));
            }


	        return awsCredentials;
	    }
	     
}
